/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utain.xmlfilewriter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

/**
 *
 * @author Nut
 */
public class UtainXMLFileWriter {

    public static void main(String[] args) {
        String word[] = {
            "Time is more value than money, You can get more money, but you cannot get more time.",
            "เมื่อทำอะไรสำเร็จ แม้เป็นก้าวเล็กๆ ของตัวเอง ก็ควรรู้จักให้รางวัลตัวเองบ้าง",
            "You will never find time for anything. if you want the time, you must make it",
            "คนส่วนใหญ่เรียกร้องสิทธิมนุษยชน แต่คนมีปัญญาเรียกร้องสิทธิ์ที่จะไม่ทุกข์"};
        String by[] = {"Jim Rohn", "ว. วชิรเมธี", "Charles Buxton", "ว. วชิรเมธี"};
        try {
            createRssDoc(word, by);
            System.out.println("Saved......");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createRssDoc(String[] word, String[] by) throws Exception {

        //bluiding
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder df = dbf.newDocumentBuilder();
        Document doc = df.newDocument();
        Element quotesRoot = doc.createElement("quotes");
        doc.appendChild(quotesRoot);
        {
            //create quote
            for (int i = 0; i < by.length; i++) {
                Element quote = doc.createElement("quote");
                quotesRoot.appendChild(quote);
                {
                    Element words = doc.createElement("words");
                    quote.appendChild(words);
                    Text wordsT = doc.createTextNode(word[i]);
                    words.appendChild(wordsT);

                    Element bys = doc.createElement("by");
                    quote.appendChild(bys);
                    Text bysT = doc.createTextNode(by[i]);
                    bys.appendChild(bysT);

                }
            }
        }

        TransformerFactory tFact = TransformerFactory.newInstance();
        Transformer trans = tFact.newTransformer();
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        DOMSource source = new DOMSource(doc);
        trans.transform(source, result);

        File file = new File("quotes.xml");
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        bw.write(writer.toString());
        bw.flush();
        bw.close();
    }

}
